package tr.com.netgis;

import java.util.ArrayList;

import tr.com.netgis.db.NGDbHelper;
import tr.com.netgis.db.NGDbHelper.Table;
import tr.com.netgis.pojo.Message;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author NetGis
 *
 */
public class MessagesListActivity extends NGListActivity {

	private String[] lv_arr = {};
	private ListView mainListView = null;
	final String SETTING_TODOLIST = "todolist";
	
	public static Message SELECTED_MESSAGE = null;
	private Button btnNew; //yeni mesaj oluştur

	private ArrayList<String> selectedItems = new ArrayList<String>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.messages);

		Button btnSave = (Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast.makeText(getApplicationContext(),
						" You clicked Save button", Toast.LENGTH_SHORT).show();

				Message m = new Message();
				m.setSender("sender " + Math.random());
				m.setCommand("command " + Math.random());
				m.setMessageHeader("Message Header " + Math.random());
				mHelper.insertMessage(m);
				mCursor.requery();
				mAdapter.notifyDataSetChanged();
				
			}
		});

		Button btnClear = (Button) findViewById(R.id.btnClear);
		btnClear.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Toast.makeText(getApplicationContext(),
						" You clicked Clear button", Toast.LENGTH_SHORT).show();
				mHelper.deleteAllMessages();
				mCursor.requery();
				mAdapter.notifyDataSetChanged();
				//ClearSelections();
			}
		});
		
		btnNew = (Button) findViewById(R.id.btnNew);
		btnNew.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent act = new Intent(MessagesListActivity.this, LocalMessageViewActivity.class);
				startActivity(act);		
			}
		});

		mainListView = getListView();
		mainListView.setCacheColorHint(0);
		mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		
		mainListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				SELECTED_MESSAGE = NGDbHelper.getMessage((Cursor)arg0.getItemAtPosition(arg2));
				Toast.makeText(getApplicationContext(),
						" Long touch", Toast.LENGTH_SHORT).show();
				Intent messageActivity = new Intent(NGApplication.getAppContext(), MessageViewActivity.class);
				startActivity(messageActivity);						
				return true;
			}
			
		});
		
		mainListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				System.out.println(arg0);
			}
			
		});

		// Bind the data with the list
		
		mainListView.setItemsCanFocus(false);
		mainListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

		//LoadSelections();
		new SelectDataTask().execute();
	}
	
	@Override
    protected void onRestart() {
        super.onRestart();
        new SelectDataTask().execute();
    }


    public void onResumezz() {
        super.onResume();
        String[] columns = NGDbHelper.getColumns(Table.MESSAGES);
        mCursor = mDb.query(NGDbHelper.TABLE_NAME_MESSAGES, columns, null, null, null, null, null, null);
        String[] headers = new String[] {NGDbHelper.COL_SENDER, NGDbHelper.COL_COMMAND};
        
        mAdapter = new SimpleCursorAdapter (
        		this, 
        		//R.layout.messages_row,
                android.R.layout.simple_list_item_checked,
        		mCursor,
                headers, 
                new int[]{android.R.id.text1, android.R.id.text2}
        );
        
        
        ((SimpleCursorAdapter)mAdapter).setViewBinder(new android.widget.SimpleCursorAdapter.ViewBinder(){
			@Override
			public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
				TextView v = (TextView)view;
				v.setLines(2);
				v.setText(Integer.toString(cursor.getInt(0)) + "\n" + "dfgdf");
				v.setTextColor(Color.BLACK);
				//v.setBackgroundColor(Color.rgb(100, 200, 50));
				return true;
			}
        });
        
        /*
        List<Message> list = mHelper.listMessages();
        mAdapter = new ArrayAdapter<Message>(this, 
        		 android.R.layout.simple_list_item_checked, list);
       	*/
        mainListView.setAdapter(mAdapter);
    }	
	
	@Override
	protected void onPause() {
		super.onPause();
		mHelper.close();
	}
	
	private class SelectDataTask extends AsyncTask<String, Void, String> {

		protected void onPreExecute() {
			// this.dialog.setMessage("Getting Names...");
			// this.dialog.show();
		}

		protected String doInBackground(final String... args) {

			try {

			} catch (SQLException sqle) {

				throw sqle;

			}
			return null;
		}

		// can use UI thread here
		protected void onPostExecute(final String result) {
			String[] columns = NGDbHelper.getColumns(Table.MESSAGES);
	        mCursor = mDb.query(NGDbHelper.TABLE_NAME_MESSAGES, columns, null, null, null, null, null, null);
	        String[] headers = new String[] {NGDbHelper.COL_SENDER, NGDbHelper.COL_COMMAND};
	        
	        mAdapter = new CustomSqlCursorAdapter(MessagesListActivity.this,
	        		R.layout.messages_row,
	        		mCursor,
	        		headers, 
	                new int[]{android.R.id.text1, android.R.id.text2},
	        		mHelper);
	        mainListView.setAdapter(mAdapter);

		}
	}
	
}
